// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#pragma once

#include <cstdint>
#include <type_traits>

#include <turbo/utility/status.h>

#include <turbo/base/macros.h>

namespace nebula {
    class DataType;

    struct ArraySpan;
    struct Scalar;

    namespace internal {
        TURBO_EXPORT
        uint8_t DetectUIntWidth(const uint64_t *values, int64_t length, uint8_t min_width = 1);

        TURBO_EXPORT
        uint8_t DetectUIntWidth(const uint64_t *values, const uint8_t *valid_bytes,
                                int64_t length, uint8_t min_width = 1);

        TURBO_EXPORT
        uint8_t DetectIntWidth(const int64_t *values, int64_t length, uint8_t min_width = 1);

        TURBO_EXPORT
        uint8_t DetectIntWidth(const int64_t *values, const uint8_t *valid_bytes, int64_t length,
                               uint8_t min_width = 1);

        TURBO_EXPORT
        void DowncastInts(const int64_t *source, int8_t *dest, int64_t length);

        TURBO_EXPORT
        void DowncastInts(const int64_t *source, int16_t *dest, int64_t length);

        TURBO_EXPORT
        void DowncastInts(const int64_t *source, int32_t *dest, int64_t length);

        TURBO_EXPORT
        void DowncastInts(const int64_t *source, int64_t *dest, int64_t length);

        TURBO_EXPORT
        void DowncastUInts(const uint64_t *source, uint8_t *dest, int64_t length);

        TURBO_EXPORT
        void DowncastUInts(const uint64_t *source, uint16_t *dest, int64_t length);

        TURBO_EXPORT
        void DowncastUInts(const uint64_t *source, uint32_t *dest, int64_t length);

        TURBO_EXPORT
        void DowncastUInts(const uint64_t *source, uint64_t *dest, int64_t length);

        TURBO_EXPORT
        void UpcastInts(const int32_t *source, int64_t *dest, int64_t length);

        template<typename InputInt, typename OutputInt>
        inline typename std::enable_if<(sizeof(InputInt) >= sizeof(OutputInt))>::type CastInts(
            const InputInt *source, OutputInt *dest, int64_t length) {
            DowncastInts(source, dest, length);
        }

        template<typename InputInt, typename OutputInt>
        inline typename std::enable_if<(sizeof(InputInt) < sizeof(OutputInt))>::type CastInts(
            const InputInt *source, OutputInt *dest, int64_t length) {
            UpcastInts(source, dest, length);
        }

        template<typename InputInt, typename OutputInt>
        TURBO_EXPORT void TransposeInts(const InputInt *source, OutputInt *dest, int64_t length,
                                        const int32_t *transpose_map);

        TURBO_EXPORT
        turbo::Status TransposeInts(const DataType &src_type, const DataType &dest_type,
                                    const uint8_t *src, uint8_t *dest, int64_t src_offset,
                                    int64_t dest_offset, int64_t length, const int32_t *transpose_map);

        /// \brief Do vectorized boundschecking of integer-type array indices. The
        /// indices must be nonnegative and strictly less than the passed upper
        /// limit (which is usually the length of an array that is being indexed-into).
        TURBO_EXPORT
        turbo::Status CheckIndexBounds(const ArraySpan &values, uint64_t upper_limit);

        /// \brief Boundscheck integer values to determine if they are all between the
        /// passed upper and lower limits (inclusive). Upper and lower bounds must be
        /// the same type as the data and are not currently casted.
        TURBO_EXPORT
        turbo::Status CheckIntegersInRange(const ArraySpan &values, const Scalar &bound_lower,
                                           const Scalar &bound_upper);

        /// \brief Use CheckIntegersInRange to determine whether the passed integers
        /// can fit safely in the passed integer type. This helps quickly determine if
        /// integer narrowing (e.g. int64->int32) is safe to do.
        TURBO_EXPORT
        turbo::Status IntegersCanFit(const ArraySpan &values, const DataType &target_type);

        /// \brief Convenience for boundschecking a single Scalar value
        TURBO_EXPORT
        turbo::Status IntegersCanFit(const Scalar &value, const DataType &target_type);

        /// Upcast an integer to the largest possible width (currently 64 bits)

        template<typename Integer>
        typename std::enable_if<
            std::is_integral<Integer>::value && std::is_signed<Integer>::value, int64_t>::type
        UpcastInt(Integer v) {
            return v;
        }

        template<typename Integer>
        typename std::enable_if<
            std::is_integral<Integer>::value && std::is_unsigned<Integer>::value, uint64_t>::type
        UpcastInt(Integer v) {
            return v;
        }
    } // namespace internal
} // namespace nebula
